//
//  UserModel.m
//  YunGou
//
//  Created by macbook on 16/6/8.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "UserModel.h"
#import <objc/runtime.h>

@implementation UserModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

- (NSString *)gender{
    if ([_gender isEqualToString:@"0"]) {
        return @"女";
    }else return @"男";
}

+ (UserModel* )sharedInstance{
    __strong static UserModel *sharedInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[UserModel alloc] init];
    });
    return sharedInstance;
}

- (void)clear{
    //  取得当前类类型
    Class cls = [self class];
    
    unsigned int ivarsCnt = 0;
    //　获取类成员变量列表，ivarsCnt为类成员数量
    Ivar *ivars = class_copyIvarList(cls, &ivarsCnt);
    
    //　遍历成员变量列表，其中每个变量都是Ivar类型的结构体
    for (const Ivar *p = ivars; p < ivars + ivarsCnt; ++p)
    {
        Ivar const ivar = *p;
        
        //　获取变量名
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        // 若此变量未在类结构体中声明而只声明为Property，则变量名加前缀 '_'下划线
        // 比如 @property(retain) NSString *abc;则 key == _abc;
        
        //　获取变量值
        id value = [self valueForKey:key];
        
        //　取得变量类型
        // 通过 type[0]可以判断其具体的内置类型
        const char *type = ivar_getTypeEncoding(ivar);
        if (value)
        {
            if (type[0] == 'c'|| type[0] == 'i' || type[0] == 's'|| type[0] == 'l'|| type[0] == 'q'|| type[0] == 'C' || type[0] == 'I'|| type[0] == 'S'|| type[0] == 'L'|| type[0] == 'Q' || type[0] == 'B'|| type[0] == 'f'||type[0] == 'd') {
                value = 0;
                
            }else{
                value = nil;
            }
            //赋值
            object_setIvar(self, ivar, value);
        }
    }
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UserModel class], &count);
    
    for (int i = 0; i<count; i++) {
        
        // 取出i位置对应的成员变量
        Ivar ivar = ivars[i];
        
        // 查看成员变量
        const char *name = ivar_getName(ivar);
        
        // 归档
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:key];
        [encoder encodeObject:value forKey:key];
    }
    
    free(ivars);
}

- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        
        unsigned int count = 0;
        Ivar *ivars = class_copyIvarList([UserModel class], &count);
        
        for (int i = 0; i<count; i++) {
            // 取出i位置对应的成员变量
            Ivar ivar = ivars[i];
            
            // 查看成员变量
            const char *name = ivar_getName(ivar);
            
            // 归档
            NSString *key = [NSString stringWithUTF8String:name];
            id value = [decoder decodeObjectForKey:key];
            
            // 设置到成员变量身上
            [self setValue:value forKey:key];
        }
        
        free(ivars);
    } 
    return self; 
}

@end
